package com.starbaba.core.utils.io;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

/**
 * 
 *
 * @Copyright: Copyright (c) 2006
 * @Company: Revenco Tech Ltd.
 * @anthor chenminhui<chenminhui@revenco.com>
 * @since 2011-12-30 下午11:15:53
 * @version V1.0
 */
public class FtpIOUtil extends BaseIOUtil {
	
	private static Log log = LogFactory.getLog(FtpIOUtil.class);
	
	/**
	 * 生成目标目录结构
	 * 
	 * @param urlDir 目标目录
	 * @throws IOException
	 *
	 * @Copyright: Copyright (c) 2006
	 * @Company: Revenco Tech Ltd.
	 * @anthor chenminhui<chenminhui@revenco.com>
	 * @since 2011-12-31 上午12:28:33
	 * @version V1.0
	 */
	public static void mkdirs(String urlDir) throws IOException {
		URL uri = new URL(urlDir);
		FTPClient fc = new FTPClient();
		try {
			fc.connect(uri.getHost(), uri.getPort());
			String[] items = StringUtils.splitPreserveAllTokens(uri.getUserInfo(), ":");
			fc.login(items[0], items[1]);
			fc.setSoTimeout(5000);//设置连接超时时间为5000毫秒
			// 在尝试连接以后，你应该检查返回码验证是否连接成功！
			int reply = fc.getReplyCode();
			if (FTPReply.isPositiveCompletion(reply)) {
//				log.info(IOUtilFactory.SYSTEM_FILE_SEPARATOR);
//				boolean isCreated = fc.makeDirectory(uri.getPath());
//				log.info("Reply Code Of Create Directory Command: " + isCreated);
				
				String[] paths = StringUtils.splitPreserveAllTokens(uri.getPath(), "/");
				for(int i = 0; i < paths.length; i++) {//循环每一个ftp目录，确保ftp目录是存在，否则创建目录
					if(StringUtils.isNotBlank(paths[i]) && !fc.changeWorkingDirectory(paths[i])) {
						boolean isCreated = fc.makeDirectory(paths[i]);
						fc.changeWorkingDirectory(paths[i]);
						log.info("Reply Code Of Create Directory Command[" + paths[i] + "]: " + isCreated);
					}
				}
				
			} else {
				log.error("Login failure!");
			}
		} finally {
			if(fc != null && fc.isConnected()) {
				fc.logout();
				fc.disconnect();
			}
		}
	}
	
	/**
	 * 打开远程目标文件，若目标文件父目录不存在，则创建，最后打开目标文件输出流
	 * 
	 * @param urlSavePath 远程目标文件目录
	 * @param filename 目标文件名
	 * @return OutputStream 目标文件输出流
	 * @throws IOException
	 *
	 * @Copyright: Copyright (c) 2006
	 * @Company: Revenco Tech Ltd.
	 * @anthor chenminhui<chenminhui@revenco.com>
	 * @since 2011-12-31 上午12:28:36
	 * @version V1.0
	 */
	@Override
	public OutputStream openOutputStream(String urlSavePath, String filename) throws IOException {
		//首先创建父目录
		mkdirs(urlSavePath);
		
		//打开FTP连接
		URL uri = new URL(urlSavePath + IOUtilFactory.SYSTEM_FILE_SEPARATOR + filename);
		return uri.openConnection().getOutputStream();
	}
	
	/**
	 * 打开远程目标文件输入流
	 * 
	 * @param urlFilePath 远程目标文件
	 * @return InputStream 目标文件输入流
	 * @throws IOException
	 *
	 * @Copyright: Copyright (c) 2006
	 * @Company: Revenco Tech Ltd.
	 * @anthor chenminhui<chenminhui@revenco.com>
	 * @since 2011-12-31 上午12:28:39
	 * @version V1.0
	 */
	@Override
	public InputStream openInputStream(String urlFilePath) throws IOException {
		//打开FTP连接
		URL uri = new URL(urlFilePath);
		return uri.openStream();
	}
}
